<?php

namespace app\controllers;

use app\models\Qa_tag;
use app\models\Tags;
use app\models\Article_tag;
use yii\data\Pagination;
use app\controllers\BaseController;

class TagsController extends BaseController {

    public function actionIndex() {
        //获取所有标签
        $query = Tags::find();
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        $tags = $query->offset($pages->offset)
                ->limit($pages->limit)
                ->all();
        return $this->render('index', ['tags' => $tags, 'pages' => $pages]);
    }

    /**
     * 根据标签ID获取问答列表
     * @param int $id
     * @return type
     */
    public function actionShow($id) {
        //获取问答列表
        $query = Qa_tag::find()->where(['tid' => $id])->with('qa');
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        $qa_list = $query->offset($pages->offset)
                ->limit($pages->limit)
                ->all();
        //获取tag详情
        $tag = Tags::findOne(['id' => $id]);
        return $this->render('show', ['qa_list' => $qa_list, 'pages' => $pages, 'tag' => $tag]);
    }

    /**
     * 根据标签ID获取文章列表
     * @param int $id
     * @return type
     */
    public function actionArticle($id) {
        //获取问答列表
        $query = Article_tag::find()->where(['tid' => $id])->with('article');
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        $article_list = $query->offset($pages->offset)
                ->limit($pages->limit)
                ->all();

        //获取tag详情
        $tag = Tags::findOne(['id' => $id]);
        return $this->render('article', ['article_list' => $article_list, 'pages' => $pages, 'tag' => $tag]);
    }

}
